package com.hy.chanping.product.user.controllers;

import com.cqsudu.lightning.service.core.Result;
import com.hy.chanping.product.core.user.dtos.SimpleUser;
import com.hy.chanping.product.core.user.dtos.User;
import com.hy.chanping.product.core.user.dtos.UserCreate;
import com.hy.chanping.product.core.user.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api")
public class UserOpenController {

    @Autowired
    private UserService userService;

    /**
     * 用户
     *
     * @param id 主键
     * @return 结果
     */
    @GetMapping(value = "/user.json")
    public Result<SimpleUser> findOne(@RequestParam(value = "id") Long id) {
        SimpleUser user = userService.findOne(id);
        return Result.result(user);
    }

    @GetMapping(value = "/users.json")
    public Result<SimpleUser> findSimpleByIds(@RequestParam(value = "ids") List<Long> ids) {
        List<SimpleUser> users = userService.findSimpleByIds(ids);
        return Result.list(users);
    }

    @PostMapping(value = "/user.json")
    public Result<Long> create(@RequestBody UserCreate user) {
        return Result.result(userService.create(user));
    }

    @GetMapping(value = "/users/page.json")
    public Result<User> page(@RequestParam(value = "name") String name,
                             @RequestParam(value = "page") Integer page,
                             @RequestParam(value = "size") Integer size) {
        return userService.page(name, page, size);
    }
}
